<template>
  <div class="assets">
    <div style="display: flex;">
      <jc-button
        :btn-arr="['reload', 'save', 'sub', 'pass', 'list']"
        :disab-arr="[isDisabled && 'save']"
        :list="'/depAdjustmentList'"
        @refresh="assetsDetails"
        @save="save"
        @submit="save(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
      />
      <!--  计提 反计提  -->
      <el-dropdown style="margin-left: 10px;">
        <el-button type="warning" size="mini" @click.native="provision(1)">
          {{ $t('market.provision') }}<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="provision(1)">{{ $t('market.provision') }}</el-dropdown-item>
          <el-dropdown-item @click.native="provision(2)">{{ $t('market.notprovision') }}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-button style="margin-left: 10px;" type="primary" size="mini" @click="proVoucher">计提生成凭证</el-button>
    </div>
    <div class="title">{{ $t('market.info') }}</div>
    <el-form :model="proValue" label-width="100px" size="mini">
      <el-form-item :label="$t('shop.state')">
        <el-tag :type="proValue.stateColor">{{ proValue.fdocumentstatus }}</el-tag>
      </el-form-item>
      <el-form-item :label="$t('market.billNo')">
        <el-input v-model="proValue.fbillno" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.orderDate')">
        <el-input v-model="proValue.fdate" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.policy')">
        <el-input v-model="proValue.fpolicyname" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.annual')">
        <el-input v-model="proValue.fdepyear" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.period')">
        <el-input v-model="proValue.fdepmon" disabled />
      </el-form-item>
      <el-form-item :label="$t('market.currency2')">
        <el-input v-model="proValue.fcurname" disabled />
      </el-form-item>
    </el-form>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.excruciating')" name="first">
        <jc-table
          serial
          :table-header="tableHeader"
          :table-data="tableDate"
          table-height="calc(100vh - 270px)"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  createAntiAccruedDepClosetask, createVoucherByAccrual, noreviewAccrued,
  nosubmitAccrued, queryAccruedDetails,
  reviewAccrued, submitAccrued,
  tAccruedDepClosetask, updateAssetsChange
} from '@/api/financial/assets'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'
import jcButton from '@/components/Button'

export default {
  components: {
    jcButton
  },
  data() {
    return {
      activeName: 'first',
      assetsVis: false,
      proValue: {},
      isDisabled: false,
      tableDate: [],
      tableHeader: [
        { label: this.$t('market.cardNumber'), prop: 'fassetsbillno', width: '200' },
        { label: this.$t('market.cardName'), prop: 'fassetsname', width: '200' },
        { label: this.$t('market.depreciationAmount'), prop: 'feverymondepprice', width: '200' },
        { label: this.$t('market.depreciationAmount2'), prop: 'feverymondepprice', width: '200' },
        { label: this.$t('market.depreciation'), prop: 'fnowmodel', width: '200' },
        { label: this.$t('market.assetstype'), prop: 'fassetstypename', width: '200' }
      ]
    }
  },
  created() {
    if (this.$route.params.id) {
      this.assetsDetails()
    }
  },
  methods: {
    // 详情
    assetsDetails() {
      const fid = this.$route.params.id
      queryAccruedDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.proValue = toMxBmina(res.data)
          this.tableDate = res.data.details
          this.tableDate.map(res => {
            res.fnowmodel = '平均年限法'
          })
          this.isDisabled = this.proValue.fdocumentstatus === this.$t('market.state1') || this.proValue.fdocumentstatus === this.$t('market.state2')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    save(ev) {
      const DATA = {
        ...notoMxBmina(this.proValue),
        details: this.tableDate
      }
      updateAssetsChange(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.assetsDetails()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.proValue.fbillno] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.assetsDetails()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitAccrued : nosubmitAccrued
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewAccrued : noreviewAccrued
      this.toExamineFun(api)
    },
    // 计提
    provision(ev) {
      const api = ev === 1 ? tAccruedDepClosetask : createAntiAccruedDepClosetask
      this.toExamineFun(api)
    },
    // 计提生成凭证
    proVoucher() {
      createVoucherByAccrual({ fid: this.$route.params.id }).then(res => {
        if (res.code === 0) {
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.assets {
  @include listBom;
  .title {
    font-size: 14px;
    margin: 5px 0;
    color: #1e9fff;
    border-bottom: 1px solid #ddd;
    padding: 0 0 3px;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 30px !important;
      width: 275px;
      margin-bottom: 5px;
    }
  }
}
</style>

